package com.ruihang.generalibrary.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatDialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Toast;

import com.ruihang.generalibrary.utils.GeneraLifeObser;

/**
 * 所有dialogfragment的基类
 * Created by Ly on 2016/10/27.
 */
public abstract class BaseDialogFragment extends AppCompatDialogFragment {

    public static final int WHAT_DISMISS = -2;

    public interface ICallBack {
        void onCallBackMsg(BaseDialogFragment fragment, Message msg);
    }

    /**
     * 是否不显示原生diaolog的title
     */
    protected boolean isNoDefTitle = true;

    protected ICallBack callBack;

    protected String baseUrl;

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public ICallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(ICallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_ATTACH);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_CREATED);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=super.onCreateView(inflater, container, savedInstanceState);
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_CREATED_V);
        return v;
    }

    @Override
    public void onStart() {
        super.onStart();
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_START);
    }

    @Override
    public void onResume() {
        super.onResume();
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_RESUME);
    }

    @Override
    public void onPause() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_PAUSE);
        super.onPause();
    }

    @Override
    public void onStop() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_STOP);
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_DESTORY_V);
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_DESTORY);
        GeneraLifeObser.removeLife(this);
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_DETACH);
        super.onDetach();
    }

    @NonNull
    @Override
    public final Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = createDialog(savedInstanceState);
        int res = getContentRes();
        if (isNoDefTitle) {
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        } else {
            if (Build.VERSION.SDK_INT >= 23) {

            }
        }
        if (res != 0) {
            dialog.setContentView(res);
        }
        initViews(dialog, savedInstanceState);
        return dialog;
    }

    protected Dialog createDialog(Bundle savedInstanceState) {
//        return new Dialog(getActivity(), R.style.CommonDialogStyle);
        return super.onCreateDialog(savedInstanceState);
    }

    @LayoutRes
    protected abstract int getContentRes();

    protected abstract void initViews(Dialog dialog, Bundle savedInstanceState);

    protected void toast(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
    }

    public String getOwnerName() {
        return this + "";
    }

    public boolean isShowing() {
        return getDialog() != null && getDialog().isShowing();
    }

    protected void toast(@StringRes int str) {
//        if(TextUtils.isEmpty(str)){
//            return;
//        }
        try {
            Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 为了解决:mainActivity调用onSaveInstanceState以后又调用了show方法,
     * 出现的Can not perform this action after onSaveInstanceState
     * 这个异常(不应该用commit ,而是用commitAllowingStateLoss)
     * 得罪了,不会反射 ,先把你catch住吧.乖
     * @param manager
     * @param tag
     */
    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            super.show(manager, tag);
        } catch (IllegalStateException ignore) {
            //  容错处理,不做操作
        }
    }

    @Override
    public void dismissAllowingStateLoss() {
        if ( getActivity() != null && !getActivity().isFinishing()) {
            super.dismissAllowingStateLoss();
        }
    }
}
